package com.topvision.controller.zwan;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.topvision.controller.AbstractController;
import com.topvision.model.zwan.*;
import com.topvision.service.zwan.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topvision.util.PageUtils;
import com.topvision.util.Query;
import com.topvision.util.R;

/**
 * 
 *
 * @author shengwm
 * @email 179653076@qq.com
 * @date 2017-08-07 13:32:20
 */
@RestController
@RequestMapping("zwan/companyinfo")
public class CompanyinfoController extends AbstractController {
    @Autowired
    private CompanyinfoService companyinfoService;
    @Autowired
    private CompanydeviceService companydeviceService;

    @Autowired
    private CompanytrunkinfoService companytrunkinfoService;

    @Autowired
    private UserinfoService userinfoService;

    @Autowired
    private SysorderdeviceinfoService sysorderdeviceinfoService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("zwan/companyinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);

        List<Companyinfo> companyinfoList = companyinfoService.queryList(query);
        int total = companyinfoService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(companyinfoList, total, query.getLimit(), query.getPage());

        return R.success().put("page", pageUtil);
    }

    /**
     * 列表
     */
    @RequestMapping("/findByParam")
    @RequiresPermissions("zwan/companyinfo:list")
    public R findByQuery(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);

        List<Companyinfo> companyinfoList = companyinfoService.queryByParam(query);
        int total = companyinfoService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(companyinfoList, total, query.getLimit(), query.getPage());

        return R.success().put("page", pageUtil);
    }

    /**
     * 列表
     */
    @RequestMapping("/companyList")
    @RequiresPermissions("zwan/companyinfo:list")
    public R companyList(@RequestParam Map<String, Object> params){
        List<Map<String,Object>> maps = companyinfoService.allList();
        return R.success(maps);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{companyid}")
    @RequiresPermissions("zwan/companyinfo:info")
    public R info(@PathVariable("companyid") BigDecimal companyid){
        Companyinfo companyinfo = companyinfoService.queryObject(companyid);
        return R.success().put("companyinfo", companyinfo);
    }

    /**
     * 绑定设备
     */
    @RequestMapping("/bindDeviceList/{companyid}")
    public R companydevices(@PathVariable("companyid") BigDecimal companyid,@RequestParam Map<String, Object> params){
        Map<String,Object> map = new HashMap<>();
        map.put("companyid",companyid);
        params.put("companyid",companyid);


        Query query = new Query(params);

        List< Map<String,Object>> companyinfoList = companydeviceService.queryBindList(query);
        int total = companydeviceService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(companyinfoList, total, query.getLimit(), query.getPage());

        return R.success().put("page", pageUtil);



    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("zwan/companyinfo:save")
    public R save(@RequestBody Companyinfo companyinfo) {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("companyname", companyinfo.getCompanyname());
        params.put("companycontact", companyinfo.getCompanyaccount());
        params.put("companyphoneno", companyinfo.getPhoneno());
        params.put("companyemail", companyinfo.getEmail());
        System.out.println(companyinfo.getEmail());
       int i = companyinfoService.addCompany(params);
        String result = params.get("msgid").toString();
        if (result.equals("405")) {
            return R.success();
        } else {
            return R.error();
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("zwan/companyinfo:update")
    public R update(@RequestBody Companyinfo companyinfo){
		companyinfoService.update(companyinfo);
        return R.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("zwan/companyinfo:delete")
    public R delete(@RequestBody BigDecimal[] companyids){
		companyinfoService.deleteBatch(companyids);
        return R.success();
    }

    /**
     * 未绑定设备列表
     */
    @RequestMapping("/notBindList")
    public R notBindList(){
        List<Sysorderdeviceinfo> sysorderdeviceinfoList = sysorderdeviceinfoService.notBindList();
        return R.success().put("sysorderdeviceinfoList",sysorderdeviceinfoList);
    }

    /**
     * 激活设备
     */
    @RequestMapping("/activeDevice")
    public R activeDevice(@RequestParam Map<String, Object> params){
        Map<String,Object> map = new HashMap<>();
        String companyId = params.get("companyid").toString().trim();
        String deviceVendorId = params.get("devicevendorid").toString().trim();
        String deviceType = params.get("devicetype").toString().trim();
        String deviceId = params.get("deviceid").toString().trim();

        params.put("companyid", companyId);
        params.put("devicevendorid", deviceVendorId);
        params.put("devicetype", deviceType);
        params.put("deviceid", deviceId);
        String returnMsg = "";
        //生成设备端口号和密码，并存入相关表中
        companytrunkinfoService.activeDevice(params);

        returnMsg = params.get("msg").toString();
        String result = params.get("msgid").toString();//存储过程返回到map中的值

        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("msg", returnMsg);


        if(result.equals("0")) {//添加设备成功
            return R.success();
        }else { //设备添加失败
            return R.error().put("msg",params.get("msg").toString());
        }
    }

    /**
     * 解除绑定
     */
    @RequestMapping("/delDevice")
    public R delDevice(@RequestParam Map<String, Object> params){


        String companyid = params.get("companyid").toString();
        String deviceid = params.get("deviceid").toString();
        System.out.println(companyid + "  " + deviceid);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("companyid", companyid);
        map.put("deviceid",deviceid);


        companydeviceService.deleteCompanyDevice(map);
        String result = map.get("msgid").toString();
        System.out.println("msgid :" + result);

        if(result.equals("0")) {
            return R.success().put("msg","删除企业设备成功");
        } else {
            return R.error();
        }
    }

}
